# Stop Services
Import-Module WebAdministration
Stop-WebItem IIS:\AppPools\ICNETRest
Stop-WebItem IIS:\AppPools\ICNETServices
& iisreset /STOP
# Delete Stuff
if(Test-Path "C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\deadletterservice")
{
	del "C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\deadletterservice" -Force -Recurse
}
if(Test-Path "C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\deprocessingservice")
{
	del "C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\deprocessingservice" -Force -Recurse
}
if(Test-Path "C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\exceptionservice")
{
	del "C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\exceptionservice" -Force -Recurse
}
if(Test-Path "C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\federationservice")
{
	del "C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\federationservice" -Force -Recurse
}
#TODO: This is legacy
if(Test-Path "C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\flexviewer")
{
	del "C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\flexviewer" -Force -Recurse
}
if(Test-Path "C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\keywordroutingservice")
{
	del "C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\keywordroutingservice" -Force -Recurse
}
if(Test-Path "C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root")
{
	del "C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root" -Force -Recurse
}
del c:\inetpub\icnet\*.aspx -Force
del c:\inetpub\icnet\*.png -Force
del c:\inetpub\icnet\*.cs -Force
del c:\inetpub\icnet\*.svc -Force
del c:\inetpub\icnet\*.Master -Force
del c:\inetpub\icnet\*.config -Force
del c:\inetpub\icnet\bin\*.* -Force
del c:\inetpub\icnet\Images\*.* -Force
del c:\inetpub\icnet\Resources\*.* -Force
del c:\inetpub\icnet\Scripts\*.* -Force
del c:\inetpub\icnet\Styles\*.* -Force
del c:\inetpub\icnet\Styles\images\*.* -Force
del c:\inetpub\icnet\XSLT\*.* -Force
del c:\inetpub\icnetsvc\DEProcessingService\Bin\*.* -Force 
del c:\inetpub\icnetsvc\ExceptionService\Bin\*.* -Force 
del c:\inetpub\icnetsvc\KeywordRoutingService\Bin\*.* -Force 
del c:\inetpub\icnetsvc\FederationService\Bin\*.* -Force 
del c:\inetpub\icnetsvc\DeadLetterService\Bin\*.* -Force 
# REST
#TODO: Make Deploy package match new structure
cp .\Deploy\wwwroot\*.aspx c:\inetpub\icnet\ -Force 
cp .\Deploy\wwwroot\*.png c:\inetpub\icnet\ -Force 
cp .\Deploy\wwwroot\*.cs c:\inetpub\icnet\ -Force 
cp .\Deploy\wwwroot\*.svc c:\inetpub\icnet\ -Force 
cp .\Deploy\wwwroot\*.Master c:\inetpub\icnet\ -Force 
cp .\Deploy\wwwroot\web.config c:\inetpub\icnet\ -Force 
cp .\Deploy\wwwroot\bin\*.* c:\inetpub\icnet\Bin -Force 
cp .\Deploy\wwwroot\Images\*.* c:\inetpub\icnet\Images -Force 
cp .\Deploy\wwwroot\bin\*.* c:\inetpub\icnet\Bin -Force 
cp .\Deploy\wwwroot\Resources\*.* c:\inetpub\icnet\Resources -Force
cp .\Deploy\wwwroot\Scripts\*.* c:\inetpub\icnet\Scripts -Force
cp .\Deploy\wwwroot\Styles\*.* c:\inetpub\icnet\Styles -Force
cp .\Deploy\wwwroot\Styles\images\*.* c:\inetpub\icnet\Styles\images -Force
cp .\Deploy\wwwroot\XSLT\*.* c:\inetpub\icnet\XSLT -Force 
cp .\Deploy\wwwroot\DEProcessingService\bin\*.* c:\inetpub\icnetsvc\DEProcessingService\Bin -Force 
cp .\Deploy\wwwroot\ExceptionService\bin\*.* c:\inetpub\icnetsvc\ExceptionService\Bin -Force 
cp .\Deploy\wwwroot\KeywordRoutingService\bin\*.* c:\inetpub\icnetsvc\KeywordRoutingService\Bin -Force 
cp .\Deploy\wwwroot\FederationService\bin\*.* c:\inetpub\icnetsvc\FederationService\Bin -Force 
cp .\Deploy\wwwroot\DeadLetterService\bin\*.* c:\inetpub\icnetsvc\DeadLetterService\Bin -Force 
# Config
#c:\ICNET\Config\ICNET_SOA_Constants.xml
if(!(Test-Path c:\ICNET\Config\ICNET_SOA_Constants.xml))
{cp .\Deploy\Config\*.* c:\ICNET\Config -Force ;break}
#log4net configs
cp .\Deploy\Config\*.config c:\ICNET\Config -Force
Remove-Item c:\ICNET\ICNETPoller -Force -Recurse
md c:\ICNET\ICNETPoller
Copy-Item .\Deploy\ICNETPoller\* c:\ICNET\ICNETPoller -Force -Recurse
Remove-Item c:\ICNET\HttpLoader -Force -Recurse
md c:\ICNET\HttpLoader
Copy-Item .\Deploy\HttpLoader\* c:\ICNET\HttpLoader -Force -Recurse
Remove-Item c:\ICNET\RouterTool -Force -Recurse
md c:\ICNET\RouterTool
Copy-Item .\Deploy\RouterTool\* c:\ICNET\RouterTool -Force -Recurse
& c:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe /u c:\ICNET\ICNETCoreRouter\ICNETCoreRouter.exe
Remove-Item c:\ICNET\ICNETCoreRouter -Force -Recurse
md c:\ICNET\ICNETCoreRouter
cp .\Deploy\ICNETCoreRouter\*.* c:\ICNET\ICNETCoreRouter\ -Force
& c:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe c:\ICNET\ICNETCoreRouter\ICNETCoreRouter.exe
& net start "IC.NET Core Router"
& iisreset /START
Start-WebItem IIS:\AppPools\ICNETRest
Start-WebItem IIS:\AppPools\ICNETServices

Write-Host "Done, press any key to continue ..."

$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")